/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package BookTrading;


import jade.core.AID;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
  @author Giovanni Caire - TILAB
 */
class BookEncheresGUI extends JFrame {	
	private BookEncheresAgent myAgent;
	
	private JTextField sellerField, buyerField,typeenchereField,nom_enchereField;
	
	BookEncheresGUI(BookEncheresAgent a) {
		super(a.getLocalName());
		
		myAgent = a;
		
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(4, 2));
		
                p.add(new JLabel("Nom de l'enchère :"));
		nom_enchereField = new JTextField(15);
		p.add(nom_enchereField);
                
                p.add(new JLabel("Nombre de vendeur(s) :"));
		sellerField = new JTextField(15);
		p.add(sellerField);
		p.add(new JLabel("Nombre d'acheteur(s) :"));
		buyerField = new JTextField(15);
		p.add(buyerField);
                
                    //il faudra programmer une menu liste déroulante le user selectionne
                p.add(new JLabel("Type d'enchères : 1- Vicrey"));
		typeenchereField = new JTextField(15);
		p.add(typeenchereField);
		getContentPane().add(p, BorderLayout.CENTER);
		
		JButton addButton = new JButton("Crée l'enchère");
		addButton.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				try {
					String nbr_seller = sellerField.getText().trim();
					String nbr_buyer = buyerField.getText().trim();
                                        String type_enchere = typeenchereField.getText().trim();
                                        String nom_enchere = nom_enchereField.getText().trim();



                                        //myAgent.updateCatalogue(Integer.parseInt(nbr_seller), Integer.parseInt(nbr_buyer));
		myAgent.create_encheres(nom_enchere,Integer.parseInt(nbr_seller), 
						Integer.parseInt(nbr_buyer),Integer.parseInt(type_enchere));
					
					sellerField.setText("");
					buyerField.setText("");
					typeenchereField.setText("");
					nom_enchereField.setText("");

				}
				catch (Exception e) {
					JOptionPane.showMessageDialog(BookEncheresGUI.this, "Invalid values. "+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 
				}
			}
		} );
		p = new JPanel();
		p.add(addButton);
		getContentPane().add(p, BorderLayout.SOUTH);
		
		// Make the agent terminate when the user closes 
		// the GUI using the button on the upper right corner	
		addWindowListener(new	WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				myAgent.doDelete();
			}
		} );
		
		setResizable(false);
	}
	
	public void showGui() {
		pack();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int centerX = (int)screenSize.getWidth() / 2;
		int centerY = (int)screenSize.getHeight() / 2;
		setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);
		super.setVisible(true);
	}	
}
